﻿using System;
using System.Data;
using System.Data.Common;
using QzEbook.Common;
using System.Collections.Generic;

/// <summary>
///PageInfo 的摘要说明
/// </summary>
/// 
namespace QzEbook.AccessObject
{
    public class BookPage : abstractAccess<BookPage> 
    {
        private int userId;
        private int bookId;
        private int pageId;
        private DateTime postDate;

        
        private int index;
        private string title;
        private string path;
        private double bytes;

        #region 属性;

        public int UserId
        {
            get { return userId; }
            set { userId = value; }
        }
        public int BookId
        {
            get { return bookId; }
            set { bookId = value; }
        }
        public int PageId
        {
            get { return pageId; }
            set { pageId = value; }
        }
        public int Index
        {
            get { return index; }
            set { index = value; }
        }
        public string Title
        {
            get { return title; }
            set { title = value; }
        }
        public string Path
        {
            get { return path; }
            set { path = value; }
        }
        public double Bytes
        {
            get { return bytes; }
            set { bytes = value; }
        }
        public DateTime PostDate
        {
            get { return postDate; }
            set { postDate = value; }
        }

        #endregion

        public BookPage()
        {
        }
        public string selectPath()
        {
            DbCommand command = CreateCommand("SelectPage");
            addParameter(command, "@PID", PageId);
            return DataAccess.ExcuteScalar(command);
        }

        protected override void bindDefaultParameter(DbCommand command)
        {
            addParameter(command,"@BID",BookId);
            addParameter(command, "@PTitle", Title);
            addParameter(command, "@PPath", Path);
        }


        protected override BookPage bindRow(DataRow row)
        {
            BookPage pages = new BookPage();
            pages.pageId = int.Parse(row["PID"].ToString());
            pages.Path = row["PPath"].ToString();
            pages.Title = row["PTitle"].ToString();
            pages.Index = int.Parse(row["PIndex"].ToString());
            pages.Bytes = (double)row["PByte"];
            pages.PostDate = DateTime.Parse(row["PDate"].ToString());
            return pages;
        }

        protected override DataTable selectTable()
        {
            DbCommand command = CreateCommand("SelectBookPage");
            addParameter(command, "@BID", BookId);
            return DataAccess.ExcuteSelectCommand(command);
        }

        /// <summary>
        /// 重载插入页;
        /// </summary>
        /// <param name="pageId">返回页索引(在书中的)</param>
        /// <returns></returns>
        public bool insert(ref int pageId)
        {
            DbCommand command = CreateCommand("InsertPage");
            this.bindDefaultParameter(command);

            addParameter(command, "@PByte", Bytes);
            addParameter(command,"@UID",UserId);
            addParameter(command, "@PID", PageId,ParameterDirection.Output);

            int i = -1;
            try
            {
                i = DataAccess.ExecuteNonQuery(command);
                pageId = int.Parse(command.Parameters["@PID"].Value.ToString());
            }
            catch { }

            return (i != -1);
        }


        public override bool update()
        {
            DbCommand command = CreateCommand("UpdatePage");
            addParameter(command, "@PID", PageId);
            addParameter(command,"@PIndex",Index);
            addParameter(command, "@PTitle", Title);
     
            int i = -1;
            try
            {
                i = DataAccess.ExecuteNonQuery(command);
            }
            catch { }

            return (i != -1);
        }

        public override bool delete()
        {
            DbCommand command = CreateCommand("DeletePage");
            addParameter(command,"@PID",PageId);
            addParameter(command, "@UID", UserId);

            int i = -1;
            try
            {
                i = DataAccess.ExecuteNonQuery(command);
            }
            catch { }
            return (i != -1);
        }
    }
}
